package fei.yuzhi.com.loveflowers.fragment.mefragment.login.register;

import android.content.Context;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Administrator on 2016/10/7.
 */
public class RegisterModel implements RegisterContract.ModelRegister{

    public interface sendSituation{
            void setSituation(String msg);
    }
    public interface sendRegister{
        void setRegister(String msg);
    }

    @Override
    public void sendYanZhengMa(Context context,String tell, final sendSituation Situation) {
//            BmobSMS.requestSMSCode(context, tell, "验证码", new RequestSMSCodeListener() {
//
//                @Override
//                public void done(Integer integer, cn.bmob.sms.exception.BmobException e) {
//                    // TODO Auto-generated method stub
//                    if (e == null) {//验证码发送成功
//                        Log.i("bmob", "短信id：" + integer);//用于查询本次短信发送详情
//                       // Toast.makeText(context, "短信已发送", Toast.LENGTH_SHORT).show();
//                        Situation.setSituation("短信已发送");
//                    }else {
//                        Log.i("bmob", "发送失败");
//                        Situation.setSituation("短信发送失败");
//                    }
//                }
//            });

    }

    @Override
    public void userRegister(final Context context, final String name, final String pass, final String tell, final RegisterModel.sendRegister register) {

        //去重复
        BmobQuery<UserBean> query = new BmobQuery<>();
        query.addWhereEqualTo("name",name);

        query.setLimit(50);
        query.findObjects(new FindListener<UserBean>() {
            @Override
            public void done(List<UserBean> list, BmobException e) {
                if (e == null && list.size() > 0) {
                    //Toast.makeText(context, "该用户名已存在", Toast.LENGTH_SHORT).show();
                    register.setRegister("该用户名已存在");
                } else {
                    //用户名不重复，创建用户
                    UserBean person = new UserBean();
                    person.setName(name);
                    person.setPass(pass);
                    person.setTell(tell);
                    person.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                               // Toast.makeText(context, "注册成功" , Toast.LENGTH_SHORT).show();
                                register.setRegister("注册成功");
                            } else {
                               // Toast.makeText(context, "创建数据失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                               // Log.i("tag", "--创建数据失败-->" + e.getMessage());
                                register.setRegister("注册失败");
                            }
                        }
                    });

                }
            }
        });

    }
}
